const app = getApp()
var api = require('../../utils/api.js')
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    limit: 10,
    dataList: [],
    noData: true,
    bindingCol:true,
    lastTime: 0,
    status:1,
    friendText:'',
    dataListCol:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          windowHeight: res.windowHeight
        })
      }
    })
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getCardList();
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      page: 1
    })
    wx.showLoading({
      title: '刷新中',
    })
    this.getCardList();
    wx.hideLoading()
    
    setTimeout(function(){
      wx.stopPullDownRefresh();
    },1000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    var page = this.data.page;
    var total_page = this.data.total_page;

    if (total_page > page) {
      this.setData({
        page: page + 1
      })
      wx.showLoading({
        title: '加载中',
      })
      this.getCardList();
    } else {
      this.setData({
        noData: false
      })
    }

  },
  getCardList: function () {
    var params = new Object()
    var that = this;
    params.page = this.data.page;
    params.limit = this.data.limit;
    if (this.data.status != undefined) {
      params.status = this.data.status;
    }
    api.POST({
      params: params,
      url: app.globalData.base_url + 'couponsbag/index',
      success: function (res) {
        
        if (util.isEmpty(res.data.code)) {
          if (res.data.data.paging.page == 1) {
            that.data.dataList=[];
          }
          var dataList = that.data.dataList.concat(res.data.data.list);
          var is_show_hint = res.data.data.is_show_hint;
          // console.log(is_show_hint);
          var bindingCol
          if (is_show_hint==1){
            bindingCol = false
          }else{
            bindingCol = true
          }
          if (dataList.length>0){
            var dataListCol = true
          }else{
            var dataListCol = false
          }
          that.setData({
            dataList: dataList,
            total_page: res.data.data.paging.total_page,
            bindingCol: bindingCol,
            is_show_bind: res.data.data.is_show_bind,
            dataListCol: dataListCol
          })
        }
      },
      fail: function (fail) {
        util.showError("登录失败，请检查网络");
        wx.stopPullDownRefresh();
        wx.hideLoading()
      }
    })
  },
  selectCid: function (e) {
    // console.log(e);
    var status = e.currentTarget.dataset.status;
    this.setData({
      page: 1,
      status: status,
      noData: true
    })
    // wx.showLoading({
    //   title: '加载中',
    // })
    this.getCardList();
  },
  //查看详情
  cardDetails: function (e) {

    var curTime = e.timeStamp;
    var lastTime = this.data.lastTime;
    this.setData({
      lastTime: curTime
    })
    if (lastTime > 0) {
      if (curTime - lastTime < 500) {
        return false;
      }
    }
    var id = e.currentTarget.dataset.id;
    var back_status = e.currentTarget.dataset.back_status;
    var status = e.currentTarget.dataset.status;
    // console.log(back_status);
    if (back_status == 0 && status==1){
      wx.navigateTo({
        url: '../cardMyDetail/cardMyDetail?id=' + id
      })
    } else if (status == 4){
      wx.navigateTo({
        url: '../repurchaseInfo/repurchaseInfo?id=' + id
      })
    }
    
  },
  //绑定手机
  phoneBinding:function(){
    wx.navigateTo({
      url: '../bindPhone/bindPhone'
    })
  },
  //取消绑定
  noBinding:function(){
    this.setData({
      bindingCol: true
    })
  },
  bindKeyInput:function(e){
    var text = e.detail.value;
    this.setData({
      friendText: text
    })
  },
  findFriends:function(){
    var text = this.data.friendText;
    if(text !=''){
      wx.navigateTo({
        url: '../findFriend/findFriend?search=' + text
      })
    }else{
      util.showError('搜索不能为空')
      return;
    }
  }
})